/**
 * @Title: UIHelper.java
 * @Package: com.jason.fingerprint.common
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年11月4日09:49:14
 * @Version: V1.0
 */
package com.jason.fingerprint.common;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.Log;

import com.jason.fingerprint.beans.ui.ImageOrAudioBean;
import com.jason.fingerprint.utils.FileUtils;

import java.io.File;
import java.util.Calendar;
import java.util.Locale;

/**
 * @ClassName: UIHelper
 * @Description: 应用文件工具
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年11月4日09:49:20
 */
public class FileHelper {

	private static final String TAG = "FileHelper";

	public static final String ROOT_FILE_NAME = "FingerPrint";// 文件存储根路径
	public static final String IMAGES_FILE_NAME = "Images";// 拍照存储路径
	public static final String AUDIOS_FILE_NAME = "Audios";// 录音存储路径
	public static final String CACHES_FILE_NAME = "Caches";// 网络图片缓存路径
	public static final String APKS_FILE_NAME = "Apks";// 应用更新存储路径
	public static final String SQL_FILE_NAME = "Sql";// 数据库保存位置
	private static File mFingerPrintDir;
	private static File mImagesDir;
	private static File mAudiosDir;
	private static File mCachesDir;
	private static File mApksDir;
	private static File mSqlDir;
	private Context mContext;

	public FileHelper(Context context) {
		this.mContext = context;
		// App缓存根目录
		mFingerPrintDir = FileUtils.getDiskCacheDir(context, ROOT_FILE_NAME);
		if (!mFingerPrintDir.exists()) {
			mFingerPrintDir.mkdirs();
		}
		// 图片
		mImagesDir = getFileFromName(mContext, IMAGES_FILE_NAME);
		if (!mImagesDir.exists()) {
			mImagesDir.mkdirs();
		}
		// 录音
		mAudiosDir = getFileFromName(mContext, AUDIOS_FILE_NAME);
		if (!mAudiosDir.exists()) {
			mAudiosDir.mkdirs();
		}
		// 网络图片
		mCachesDir = getFileFromName(mContext, CACHES_FILE_NAME);
		if (!mCachesDir.exists()) {
			mCachesDir.mkdirs();
		}
		// 软件更新
		mApksDir = getFileFromName(mContext, APKS_FILE_NAME);
		if (!mApksDir.exists()) {
			mApksDir.mkdirs();
		}
		// 数据库
		mSqlDir = getFileFromName(mContext, SQL_FILE_NAME);
		if (!mSqlDir.exists()) {
			mSqlDir.mkdirs();
		}
	}

	// 获取应用缓存根目录下
	public File getFingerPrintDir() {
		return mFingerPrintDir;
	}

	// 获取应用缓存根目录下
	public File getImagesDir() {
		return mImagesDir;
	}

	// 获取应用缓存根目录下
	public File getAudiosDir() {
		return mAudiosDir;
	}

	// 获取应用缓存根目录下
	public File getCachesDir() {
		return mCachesDir;
	}

	// 获取更新的apk
	public File getApksDir() {
		return mApksDir;
	}
	
	//数据库
	public File getSqlDir(){
		return mSqlDir;
	}

	//获取App根目录
	public static String getAppRootDir(Context context) {
		return FileUtils.getDiskCacheDir(context, ROOT_FILE_NAME)
				.getAbsolutePath();
	}

	public static String getSdRootDir(Context context) {
		return FileUtils.getDiskCacheDir(context, ROOT_FILE_NAME)
				.getAbsolutePath();
	}

	// 获取应用根目录下指定文件夹名字的文件
	public static File getFileFromName(Context context, String dirName) {
		File rootFile = FileUtils.getDiskCacheDir(context, ROOT_FILE_NAME);
		if (!rootFile.exists()) {
			rootFile.mkdirs();
		}
		File childFile = new File(rootFile, dirName);
		if (!childFile.exists()) {
			rootFile.mkdirs();
		}
		return childFile;
	}

	// 获取图片随机名字
	public static String getPhotoFileName() {
		String photoName = DateFormat.format("yyyyMMdd_hhmmss",
				Calendar.getInstance(Locale.CHINA))
				+ ".jpg";
		return photoName;
	}

	// 获取一个空的图片文件
	public static File getPhotoFile(Context context) {
		File photoFile = new File(new FileHelper(context).getImagesDir(),
				FileHelper.getPhotoFileName());
		return photoFile;
	}
	
	//获取数据库的路径
	public static String getSqlPath(Context context){
		return new FileHelper(context).getSqlDir().getPath();
	}

	/***
	 * 删除指定文件
	 * @param path
	 */
	public static void deleteFile(String path){
		if (!TextUtils.isEmpty(path)){
			try {
				File file = new File(path);
				if (file != null && file.exists()){
					file.delete();
				}
			} catch (Exception e){
				e.printStackTrace();
			}
		}
	}

	/***
	 * 获取最新拍照的图片信息
	 * @Descripton: TODO
	 * @Param @return
	 * @Return $
	 * @Throws
	 */
	public static String getTheNewPhotoPath(Context context){
		Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
		ContentResolver mContentResolver = context.getContentResolver();
		String path = null;
		try {
			Cursor mCursor = mContentResolver.query(mImageUri, null,
					null,null,
					MediaStore.Images.Media.DATE_ADDED);
			mCursor.moveToFirst();
			path = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.DATA));
			if (mCursor != null) {
				mCursor.close();
			}
		} catch (Exception e) {

		}
		return path;
	}


	/***
	 * 根据文件路径获取文件信息
	 */
	public static ImageOrAudioBean getFileBeanByPath(
			Context context, String path, int fileType) {
		Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
		ImageOrAudioBean bean = new ImageOrAudioBean();
		ContentResolver mContentResolver = context.getContentResolver();
		// 只查询jpeg和png的图片
		if (!TextUtils.isEmpty(path)){
			Cursor mCursor = mContentResolver.query(mImageUri, null,
					MediaStore.Images.Media.DATA + "=?", new String[] { path },
					MediaStore.Images.Media.DATE_MODIFIED);
			mCursor.moveToFirst();
			String title = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.TITLE));
			String disName = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
			String type = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.MIME_TYPE));
			String id = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media._ID));
			String addDate = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.DATE_ADDED));
			String modifyData = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED));
			String height = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.HEIGHT));
			String width = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.WIDTH));
			String size = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.SIZE));
			Log.i(TAG, ",path:" + path + ",title:" + title + ",disName:"
					+ disName + ",type:" + type + ",id:" + ",addDate:"
					+ addDate + ",modifyData:" + modifyData + ",height:"
					+ height + ",width:" + width + ",size:" + size);
			bean.setCreateDate(String.valueOf(System.currentTimeMillis()));
			bean.setFileName(title);
			bean.setFilePath(path);
			bean.setFileType(fileType);
			bean.setState(1);
			if (mCursor != null) {
				mCursor.close();
			}
		}
		return bean;
	}

}
